//
//  MMS.cpp
//  MMS
//
//  Created by 王翛然 on 16/8/9.
//  Copyright © 2016年 王翛然. All rights reserved.
//

#include "MMS.hpp"

void MMS::addMember(Member &mem)
{
    if (!checkMember(mem.getMemberName()))
    {
        std::string sql = "select max(MEMBER_ID) from MEMBERS";
        int memberId = 0;
        if(!executeSql(sql))
        {
            if (checkResult())
            {
                MYSQL_ROW row;
                row = mysql_fetch_row(result);
                if (*row != NULL)
                    memberId = (int)atol(row[0]) + 1;
                else
                    memberId = 1;
                mysqlFreeResult();
            }
            sql = getMemberId(memberId);
            mem.setMemberId(sql);
            updateMember(mem);
        }
    }
    else
    {
        std::cerr << "已存在用户\n";
    }
}


void MMS::updateMember(const Member &mem)
{
    std::string sql = "insert into MEMBERS values(\'";
    sql += mem.getMemberId();
    sql += "\', \'" + mem.getMemberName() + "\', \'" + mem.getAddress() + "\', " + "\'" + mem.getProfessional() + "\')";
    executeSql(sql);
    mysqlFreeResult();
}

void MMS::addCardToMember(const std::string &memberName)
{
    if (checkMember(memberName))
    {
        if (!checkCard(memberName))
        {
            std::string sql = "insert into ANNUALS values(\'" + memberName +
                                "\', " + "now());";
            if (executeSql(sql))
            {
                std::cerr << "未知错误\n";
                return;
            }
            mysqlFreeResult();
            sql = "select MEMBER_NAME, MEMBER_ID from MEMBERS where MEMBER_NAME = \'" +
                    memberName + "\'";
            if (executeSql(sql))
            {
                std::cerr << "未知错误\n";
                return;
            }
            MYSQL_ROW row;
            row = mysql_fetch_row(result);
            std::cout << "OK, A new 年卡 added to " + memberName + "(" + row[1] + ")\n";
        }
        else
        {
            std::cerr << "您已有卡\n";
        }

    }
    else
    {
        std::cout << "无此用户\n";
        return;
    }
    
}

void MMS::addCardToMember(const std::string &memberName, int times)
{
    if (checkMember(memberName))
    {
        if (!checkCard(memberName))
        {
            std::string sql = "insert into TIMES_OF_CARD values(\'" +
            memberName + "\', " + "\'" + itoa(times) + "\')";
            
            if (!executeSql(sql))
            {
                std::cout << "OK, A new 次卡 added to " + memberName << "\n";
            }
            else
                std::cout << "未知错误\n";
        }
        else
        {
            std::cerr << "您已有卡!!!";
        }
    }
    else
    {
        std::cout << "无此用户\n";
        return;
    }
}

std::string MMS::itoa(int num)
{
    std::stringstream ss;
    std::string s;
    ss << num;
    ss >> s;
    
    return s;
}

bool MMS::checkResult()
{
    bool flag = false;
    if (result->row_count != 0)
    {
        flag = true;;
    }
    return flag;
}

bool MMS::checkMember(const std::string &memberName)
{
    std::string sql = "select MEMBER_NAME from MEMBERS where MEMBER_NAME = \'" +
                        memberName + "\'";
    return check(sql);
}

bool MMS::checkCard(const std::string &memberName)
{
    std::string sql = "select * from annuals, TIMES_OF_CARD where "
                        "annuals.MEMBER_NAME = \'" +
                        memberName + "\' or TIMES_OF_CARD.MEMBER_NAME = \'" +
                        memberName + "\'";
    return check(sql);
}

bool MMS::check(const std::string &sql)
{
    if (!executeSql(sql))
    {
        if (checkResult())
        {
            mysqlFreeResult();
            return true;
        }
    }
    else
    {
        std::cerr << "未知错误\n";
        exit(0);
    }
    return false;
}

void MMS::mysqlFreeResult()
{
    mysql_free_result(result);
}

std::string MMS::getMemberId(int num)
{
    auto memberId = itoa(num);
    if (memberId.size() < 3)
    {
        std::string zero(3 - memberId.size(), '0');
        zero += memberId;
        memberId = zero;
    }
    
    return memberId;
}